package com.itita.ww2.handler.requesthandler.building;

import java.sql.SQLException;

import com.smartfoxserver.v2.entities.User;
import com.smartfoxserver.v2.entities.data.ISFSObject;
import com.smartfoxserver.v2.entities.data.SFSObject;
import com.itita.ww2.core.MessageHandler;
import com.itita.ww2.core.R;
import com.itita.ww2.core.WW2Exception;
import com.itita.ww2.enums.WW2ExceptionCode;
import com.itita.ww2.game.user.UserService;
import com.itita.ww2.game.user.UserBuildingManager.BuildingType;
import com.itita.ww2.model.user.DevTime.DevTimeType;
import com.itita.ww2.model.user.DevTime;
import com.itita.ww2.model.user.UserProfile;
import com.itita.ww2.utils.handler.IRequestOper;

/**
 * 升级固定建筑
 * @author NorthLan
 *
 */
public class BaseBDUpgrade implements IRequestOper {

	@Override
	public boolean execute(String requestCMD, User user, ISFSObject params) throws WW2Exception, SQLException {
		UserProfile userProfile = UserService.getInstance().getUserProfile(user);
		BuildingType bdType = BuildingType.codeValueOf(params.getUtfString("code"));
		// 
		ISFSObject backObj = SFSObject.newInstance();
		backObj.putInt(R.DATA_KEY_COUNTER, params.getInt(R.DATA_KEY_COUNTER));

		// 升级
		if (!userProfile.getBDManager().upgrade(bdType)) {
			throw new WW2Exception(WW2ExceptionCode.INVALID_OPT);
		}

		DevTime dt = userProfile.getTimeManager().getDevTime(DevTimeType.UPGRADE, bdType.getValue());
		ISFSObject ret = dt.toSFSObject();
		ret.putUtfString("key", dt.getKey());
		backObj.putSFSObject("devTime", ret);

		MessageHandler.getInstance().sendMsg(requestCMD, backObj, user, null, true);
		return true;
	}

}
